const VodUploader = require('../../lib/vod-web-sdk-v5.1');
const app = getApp();

Page({

	data: {
		fileName: ''
	},
	getSignature: function(callback) {
		wx.request({
			url: app.apiDomain + 'index.php?c=user&a=getUploadSign',
			method: 'POST',
			dataType: 'json',
			success: function(res) {
				// console.log(res);
				if (res.data && res.data.data) {
					callback(res.data.data);
				} else {
					return '获取签名失败';
				}
			}
		});
	},
	inputChange: function(evt) {
		this.fileName = evt.detail.value;
	},
	chooseFile: function() {
		console.log(2)
		var This = this;
		wx.chooseVideo({
			sourceType: ['album', 'camera'],
			compressed: true,
			maxDuration: 60,
			success: function(file) {
				This.setData({
					tiao: 'on'
				});
				VodUploader.start({
					videoFile: file, //必填，把chooseVideo回调的参数(file)传进来
					fileName: This.fileName, //选填，视频名称，强烈推荐填写(如果不填，则默认为“来自微信小程序”)
					getSignature: This.getSignature, //必填，获取签名的函数
					success: function(result) {
						// console.log('success');
						//             console.log(result);
					},
					error: function(result) {
						//             console.log('error');
						//             console.log(result);
						wx.showModal({
							title: '上传失败',
							content: JSON.stringify(result),
							showCancel: false
						});
					},
					progress: function(result) {
						//             console.log('progress');
						// console.log(result.percent);
						This.setData({
							tiao_win: result.percent * 100
						})
					},
					finish: function(result) {
//						 console.log(result);
						// console.log(result.videoUrl);
						This.setData({
							video_url: result.videoUrl,
							fileId: result.fileId,
							video_get: 'on',
							tiao: ''
						});
						//
						app.request(app.apiDomain + "index.php?c=user&a=saveVideoFile", {
							fileId: result.fileId,
							videoName: result.videoName,
							videoUrl: result.videoUrl
						}, function(res) {
							// console.log(res);

						});
						// 
						// 
						//             wx.showToast({
						//               title: '上传成功',
						//               icon: 'none',
						//               duration: 1000
						//             });
					}
				});
			}
		})
	},
	img_load: function(e) {
		var thas = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {
				thas.img = res.tempFilePaths[0];
				// console.log(res)
				thas.img_quality(res.tempFilePaths);
			}
		})
	},
	img_quality: function(e) {
		var thas = this;
		wx.compressImage({
			src: e[0], // 图片路径
			quality: 80, // 压缩质量
			success: function(res) {
				// console.log(res.tempFilePath)
				thas.setData({
					iocn_img: res.tempFilePath,
					img_get: 'off',
				});
			}
		})
	},
	form_submit: function(e) {
		var thas = this;
		if (!this.data.video_get) {
			wx.showToast({
				title: '请上传视频',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		if (!this.data.iocn_img) {
			wx.showToast({
				title: '请上传封面',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		if (!e.detail.value.textarea) {
			wx.showToast({
				title: '请填写内容',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		if (!this.data.hua_ti_num) {
			wx.showToast({
				title: '请选择圈子',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		wx.showLoading({
			title: '发表中',
		});
		// console.log('123')
		//
		wx.uploadFile({ //提交图片
			url: app.apiDomain + "index.php?c=group&a=addArticle", //接口
			filePath: thas.data.iocn_img,
			name: 'file',
			formData: {
				content: e.detail.value.textarea,
				topic_id: thas.data.hua_ti_num,
				remark: '',
				video_url: thas.data.video_url,
				//imgs:thas.data.iocn_img,
				openid: wx.getStorageSync("openid"),
				video_id: thas.data.fileId
			},
			header: {
				"Content-Type": "application/x-www-form-urlencoded",
				//"sessionId": getApp().globalData.sessionId,
			},
			method: 'post',
			success: function(res) {
				// console.log(res);
				wx.hideLoading()
				wx.showToast({
					title: res.data.info,
					icon: 'none',
					duration: 1000
				});
				setTimeout(function() {
					wx.reLaunch({
						url: 'index'
					})
				}, 1200);
			}
		});
		// 			app.request(app.apiDomain + "index.php?c=group&a=addArticle", {
		// 					content:e.detail.value.textarea,
		// 					topic_id:thas.data.hua_ti_num,
		// 					remark:'',
		// 					imgs:thas.data.iocn_img,
		// 			}, function(res) {
		// 					console.log(res);
		// 					wx.showToast({
		// 						title: res.data.info,
		// 						icon: 'none',
		// 						duration: 1000
		// 					});
		// // 					setTimeout(function(){
		// // 							wx.reLaunch({
		// // 									url:'index'
		// // 							})
		// // 					},1500);
		// 					
		// 			});
	},
	hua_ti_click: function() {
		this.setData({
			hua_ti_on: 'on'
		})
	},
	hua_ti_off: function() {
		this.setData({
			hua_ti_on: ''
		})
	},
	hua_li_click: function(e) {
		// console.log(e)
		this.setData({
			hua_ti_on: '',
			hua_ti_wrds: e.currentTarget.dataset.wrds,
			hua_ti_num: e.currentTarget.dataset.id
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var thas = this;
		app.request(app.apiDomain + "index.php?c=group&a=topic", {}, function(res) {
			// console.log(res.data.data)
			thas.setData({
				topic: res.data.data
			});
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		return app.onShareAppMessage();
	}
})
